/**
 * Created by Administrator on 2016/1/22.
 */
var Promise = require("bluebird");
var fs = require("fs");
//fs.readFile('1.txt',)
function readFile(name){
    return new Promise(function(resolve,reject){
        fs.readFile(name,function(err,doc){
            if(err){
                reject(err);
            }else{
                resolve(doc.toString());
            }
        })
    });
}

//readFile('1.txt').then(console.log,console.log);

+function(){
    var names = ['1.txt','2.txt'];
    var promises = names.map(function(name){
        return readFile(name);
    });
    //Promise.all(promises).then(function(arr){
    //    console.log(arr);
    //})
}();

+function(){
    /*var promise1 = readFile('1.txt');
    promise1.then(function(doc1){
        console.log(doc1);
        return readFile('2.txt').then(function(doc2){
            console.log(doc2);
            return readFile('3.txt');
    })}).then(function(a){
        console.log(a);
    })*/
}();

+function(){
    var promise1 = readFile('1.txt');
    promise1.then(function(doc1){
        return Promise.all([doc1,readFile('2.txt')]);
    }).then(function(msg){
        console.log(msg);//[ '<property value={name}></property>', '2.txt' ]
    })
}();

+function () {
    (new Promise(function(resolve,reject){
        resolve(1);
    })).then(function(msg){
        console.log(msg);
        return readFile('1.txt');
    }).then(function (msg) {
        console.log(msg);
    })
}();